<?php
namespace app\merchant\controller\member;
use app\common\controller\BaseController;
use app\common\model\setting\SettingStorePlatformModel;
use app\common\service\member\MemberAccountService;
use app\common\validate\member\MemberAccountValidate;
use hg\apidoc\annotation as Apidoc;
/**
 * @Apidoc\Title("接单账号")
 * @Apidoc\Group("member")
 * @Apidoc\Sort("250")
 */
class MemberAccount extends BaseController
{
    /**
    * @Apidoc\Title("接单账号列表")
    * @Apidoc\Query(ref="pagingQuery")
    * @Apidoc\Query(ref="sortQuery")
    * @Apidoc\Query(ref="searchQuery")
    * @Apidoc\Query(ref="dateQuery")
    * @Apidoc\Returned(ref="expsReturn")
    * @Apidoc\Returned(ref="pagingReturn")
    * @Apidoc\Returned("list", type="array", desc="接单账号列表", children={
    *   @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel", field="id,title,content,is_disable,create_time,update_time,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
    * })
    */
    public function list()
    {
        $where = $this->buildWhere([
            'member_id',
            'store_platform_id',
            'setting_store_type_ids',
            'is_disable',
            'status',
            'level'
        ]);
        $where = $this->where(where_delete($where));
        $data = MemberAccountService::list($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }
    /**
     * @Apidoc\Title("查询自选买号")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="查询自选买号", children={
     *   @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel", field="id,title,content,is_disable,create_time,update_time,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
     * })
     */
    public function getAccounts()
    {
        $where = $this->buildWhere([
            'member_id',
            'setting_store_type_ids',
            'is_disable',
            'status',
            'level',
            'region_id',
            'sex'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['is_order_taking','=',1];//是否开启接单
        //查看店铺平台
        $param = $this->params([
            'store_platform_id/d' =>null,
            'integral/d'=>null,
            'taoling/d'=>null
        ]);
        if($param['store_platform_id']){
            $taobao_ids = SettingStorePlatformModel::getTaobaoIds();
            if(in_array($param['store_platform_id'],$taobao_ids)){
                $where[] = ['store_platform_id','in',$taobao_ids];
            }else{
                $where[] = ['store_platform_id','=',$param['store_platform_id']];
            }
        }
        //淘气值
        if($param['integral']!=null){
            $where[] = ['integral','>=',$param['integral']];
        }
        //淘龄
        if($param['taoling']!=null){
            $where[] = ['taoling','>=',$param['taoling']];
        }
        $data = MemberAccountService::getAccounts($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }

    /**
     * @Apidoc\Title("查询参数")
     * @Apidoc\Query(ref="app\common\model\member\MemberAccountModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel")
     */
    public function getParams()
    {
        $where = $this->where(where_delete());
        $data = MemberAccountService::getParams();
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号信息")
    * @Apidoc\Query(ref="app\common\model\member\MemberAccountModel", field="id")
    * @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel")
    */
    public function info()
    {
        $param = $this->params(['id/d' => '']);
        validate(MemberAccountValidate::class)->scene('info')->check($param);
        $data = MemberAccountService::info($param['id']);
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号添加")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="title,content,is_disable,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
    */
    public function add()
    {
        $param = $this->params(MemberAccountService::$edit_field);
        validate(MemberAccountValidate::class)->scene('add')->check($param);
        $data = MemberAccountService::add($param);
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号修改")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="id,title,content,is_disable,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
    */
    public function edit()
    {
        $param = $this->params(MemberAccountService::$edit_field);
        validate(MemberAccountValidate::class)->scene('edit')->check($param);
        $data = MemberAccountService::edit($param['id'], $param);
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号删除")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="idsParam")
    */
    public function dele()
    {
        $param = $this->params(['ids/a' => []]);
        validate(MemberAccountValidate::class)->scene('dele')->check($param);
        $data = MemberAccountService::dele($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("接单账号禁用")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="is_disable")
     */
    public function disable()
    {
        $param = $this->params(['ids/a' => [], 'is_disable/d' => 0]);
        validate(MemberAccountValidate::class)->scene('disable')->check($param);
        $data = MemberAccountService::edit($param['ids'], $param);
        return success($data);
    }

    
    /**
     * @Apidoc\Title("接单账号审核")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="status,auth_msg")
     */
    public function is_auth()
    {
        $param = $this->params(['ids/a' => [], 'status/d' => 0, 'auth_msg/s' => null]);
        validate(MemberAccountValidate::class)->scene('auth')->check($param);
        $param['auth_time'] = date('Y-m-d H:i:s');
        $param['auth_uid']  = user_id();
        $data = MemberAccountService::edit($param['ids'], $param);
        return success($data);
    }
}
